// https://www.cnblogs.com/yoona-lin/p/16401037.html
// https://blog.csdn.net/qq_41294889/article/details/115968821
// 星期格式转换 0-6：周日到周六
function weekFormat(e, prefix = "周") {
  switch (e) {
    case 0:
      return prefix + "日";
      break;
    case 1:
      return prefix + "一";
      break;
    case 2:
      return prefix + "二";
      break;
    case 3:
      return prefix + "三";
      break;
    case 4:
      return prefix + "四";
      break;
    case 5:
      return prefix + "五";
      break;
    case 6:
      return prefix + "六";
      break;
    default:
      return "";
      break;
  }
}
// 转换今日的时辰格式
function todayTimeFormat(e) {
  if (e >= 0 && e < 7) {
    return "凌晨";
  } else if (e >= 7 && e < 11) {
    return "上午";
  } else if (e >= 11 && e < 13) {
    return "中午";
  } else if (e >= 13 && e < 18) {
    return "下午";
  } else if (e >= 18 && e < 24) {
    return "晚上";
  } else {
    return "";
  }
}

// 是否显示周几
function isShowWeekDay(sub, weekDay) {
  const currentWeekDay = new Date().getDay();
  const dayTime = 1000 * 60 * 60 * 24;
  // 1.当前时间与消息时间相差必须大于2天小于7天
  // 2.当前时间距离本周一相差必须大于2天且小于当前距离周一的天数
  // 3.消息时间不可能是0-周日，因为周日没有给后面时间留空间，不会走这里的逻辑，而是走今天的逻辑
  if (sub >= dayTime * 1 && weekDay !== 0 && sub <= dayTime * currentWeekDay) {
    return true;
  } else {
    return false;
  }
}

// 仿微信时间显示格式转换 @time 时间戳毫秒
function wechatTimeFormat(time) {
  const today = new Date().getTime();
  // 当前时间减去获取到的时间
  const sub = today - time;
  const day = 1000 * 60 * 60 * 24;
  const timeDate = new Date(time);
  const currentYear = new Date().getFullYear();
  const getYear = new Date(time).getFullYear();
  const HHmm = `${formatTime2("hh", timeDate)}:${formatTime2("mm", timeDate)}`;
  const showWeekDay = isShowWeekDay(sub, timeDate.getDay());
  const currentDay = new Date().getDate();
  const getDay = new Date(time).getDate();
  const subDay = currentDay - getDay;
  console.log(currentDay, getDay, subDay);
  if (currentYear > getYear) {
    return `${formatTime2("yyyy-MM-dd", timeDate)} ${formatTime2(
      "hh:mm",
      timeDate
    )}`;
    // 带上午下午
    // return `${formatTime2("yyyy年M月d日", timeDate)} ${todayTimeFormat(
    //   timeDate.getHours()
    // )} ${formatTime2("hh:mm", timeDate)}`;
  } else if (showWeekDay) {
    // return `${weekFormat(timeDate.getDay())} ${HHmm}`;
    var showSub = "";
    switch (sub) {
      case 1:
        return "昨天";
        break;
      case 2:
        showSub = "两";
        break;
      case 3:
        showSub = "三";
        break;
      case 4:
        showSub = "四";
        break;
      case 5:
        showSub = "五";
        break;
      case 6:
        showSub = "六";
        break;
      case 7:
        showSub = "七";
        break;
      default:
        break;
    }
    return `${showSub}天前`;
    //} else if (sub > day && sub < day * 2) {
  } else if (sub <= day) {
    return HHmm;
  } else {
    return `${formatTime2("MM-dd", timeDate)} ${formatTime2(
      "hh:mm",
      timeDate
    )}`;
    // 上午下午
    // return `${formatTime2("M月d日", timeDate)} ${todayTimeFormat(
    //   timeDate.getHours()
    // )} ${formatTime2("hh:mm", timeDate)}`;
  }
}
//时间格式化兼容 ios
function formatTime2(fmt, timestamp) {
  var date = new Date(timestamp); // 兼容safari
  var o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
    "q+": Math.floor((date.getMonth() + 3) / 3),
    S: date.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}
// 比较时间大于5分钟  是否显示
function diff2time(targetTime, sourceTime) {
  var diffTime = 1000 * 60 * 5;
  if (targetTime - sourceTime > diffTime) {
    return true;
  } else {
    return false;
  }
}
module.exports = {
  weekFormat,
  todayTimeFormat,
  isShowWeekDay,
  wechatTimeFormat,
  diff2time
};
